package com.example.demo.api;

import com.example.demo.model.Friend;
import com.example.demo.mapper.FriendMapper;
import com.example.demo.model.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class FriendAPI {
    @Resource
    private FriendMapper friendMapper;

    @GetMapping("/friendList")
    @ResponseBody
    public Object getFriendList(HttpServletRequest req) {
        //1.先从会话中获取 userId 是啥
        HttpSession session = req.getSession(false);
        if(session == null) {
            //当前用户会话不存在
            //直接返回一个空的列表即可
            log.info("session 不存在");
            return new ArrayList<Friend>();
        }
        User user = (User) session.getAttribute("user");
        if(user == null) {
            //当前用户对象没在会话中
            log.info("user 不存在");
            return new ArrayList<Friend>();
        }

        //2.根据 userId 从数据库中查数据
        List<Friend> friendList = friendMapper.selectFriendList(user.getUserId());
        return friendList;
    }
}
